import mplfinance as mpf
from matplotlib.pylab import date2num
from utility.objects import Mktdata
from datetime import datetime
import pandas as pd
import os


# get mktdata
begin = datetime(2018, 1, 1)
end = datetime(2018, 7, 28)
data_path = "Y:\mktdata\okex\mkt_min"
symbol = "eth"
file_path = data_path + "\\" + symbol + ".csv"
data = Mktdata(file_path, "5T", begin, end)

mkt_min = data.mkt_day
# mkt_min[["datetime"]] = pd.to_datetime(mkt_min.datetime,format='%Y-%m-%d %H:%M:%S')
mkt_min.datetime = date2num(mkt_min.datetime.apply(pd.to_datetime, **{'format': '%Y-%m-%d %H:%M:%S'}))

mkt_temp = mkt_min.loc["2018",:]
mpf.plot(data.mkt_day,type='candle',mav = (7 ,14, 30))

# read backtesting result

# read txns
file_path = "\\output\\"
algo= 'Pro_Grid_'+ symbol
txn_file_name = r"C:\Users\Administrator\Desktop\backtest_framework\algos\output" + '\\' + algo + "_txns.csv"

txns = pd.read_csv(txn_file_name,index_col=0,parse_dates=True)

# read dailyEq

dailyEq_file_name = r"C:\Users\Administrator\Desktop\backtest_framework\algos\output" + "\\"+algo + "_daily_eq.csv"
dailyEq = pd.read_csv(dailyEq_file_name,index_col=0,parse_dates=True)


# add txns
#buys = txns.loc[txns.Txn_Qty > 0,"Txn_Price"]
#sells = txns.loc[txns.Txn_Qty < 0,"Txn_Price"]



buys = txns[["txn_price"]].loc[txns.txn_qty > 0,:]
sells = txns[["txn_price"]].loc[txns.txn_qty < 0,:]

#buys = buys['Txn_Price'].resample("1D").last()
#sells = sells['Txn_Price'].resample("1D").last()



mkt_temp["buys"] = buys['txn_price'].resample("1D").last()
mkt_temp["sells"] = sells['txn_price'].resample("1D").last()





dailyEq = dailyEq.resample("1D").last()
dailyEq.dropna(inplace = True )
dailyEq.drop(dailyEq.index[0],inplace = True)
mkt_temp["daily_eq"] = dailyEq

apds =[
       mpf.make_addplot(mkt_temp["buys"],type='scatter',markersize=200,marker='^'),
       mpf.make_addplot(mkt_temp["sells"],type='scatter',markersize=200,marker='v'),
       # mpf.make_addplot(mkt_temp["dailyEq"],type='line',panel=1)

]
mpf.plot(mkt_temp,type='candle',addplot=apds)

